<?php

namespace App\Http\Controllers;

use App\Models\LoginNum;
use App\Models\UserNum;
use App\Providers\MyJwt;
use App\Services\ManagerService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    protected $domain;

    protected $cookie;

    protected $request;

    protected $year;

    public function __construct(Request $request)
    {
        header('Access-Control-Allow-Origin:*');
        $this->request = $request;
        $this->domain = env('DOMAIN');
        $this->cookie = env('COOKIE_DOMAIN');
        $this->year = env('CREATE_YEAR');
    }

    public function check()
    {
        $this->sendGet(function(){
            $authorization = $this->request->header('Authorization');
            $jwt = new MyJwt();
            $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);

            if ($jwtCode['role'] == 3) {
                $data = UserService::getById($jwtCode['uid']);
            } else {
                $data = ManagerService::getManagerDetail($jwtCode['uid']);
            }
            $code = 200;
            $msg = 'OK';
            if (empty($data)) {
                $code = 4031;
                $msg = '请授权';
            }
            $this->sendJson([], $code, $msg);
        });
    }

    public function login()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $data = ManagerService::login($params['username'], md5($params['password']));
            $this->loginCount();
            $this->sendJson($data);
        });
    }

    protected function sendJson(array $data, $status = 200, $message = 'OK')
    {
        echo json_encode([
            'body' => count($data) ? $data : new \stdClass(),
            'status' => $status,
            'message' => $message
        ]);exit;
    }

    protected function sendDataTable(array $data)
    {
        echo json_encode($data);exit;
    }

    protected function sendGet($callback)
    {
        try {
            $callback();
        } catch(\Exception $e) {
            $this->sendJson([], $e->getCode(), $e->getMessage());
        }
    }

    protected function sendPost($callback)
    {
        try {
            $callback();
        } catch(\Exception $e) {
            $this->sendJson([], $e->getCode(), $e->getMessage());

        }
    }

    protected function baseView($view, array $params)
    {
        $token = $this->request->get('_token');
        $role = $this->request->get('role');
        return view($view, array_merge($params, ['domain' => $this->domain, 'cookie' => $this->cookie, 'token' => $token, 'role' => $role]));
    }

    protected function loginCount()
    {
        $today = date('Y-m-d', time()).' 00:00:00';
        $num = LoginNum::orderBy('id', 'desc')->first();
        if (!$num) {
            $num = new LoginNum();
            $num->login_num = 1;
            $num->login_daily = 1;
            $num->created_at = date('Y-m-d H:i:s', time());
            $num->updated_at = date('Y-m-d H:i:s', time());
            $num->save();
        } else {
            if (strtotime($num->created_at) >= strtotime($today)) {
                $num->login_num ++;
                $num->login_daily ++;
                $num->updated_at = date('Y-m-d H:i:s', time());
                $num->save();
            } else {
                $newNum = new LoginNum();
                $newNum->login_num = $num->login_num + 1;
                $newNum->login_daily = 1;
                $newNum->created_at = date('Y-m-d H:i:s', time());
                $newNum->updated_at = date('Y-m-d H:i:s', time());
                $newNum->save();
            }
        }
    }

    protected function registerCount()
    {
        $today = date('Y-m-d', time()).' 00:00:00';
        $num = UserNum::orderBy('id', 'desc')->first();
        if (!$num) {
            $num = new UserNum();
            $num->user_num = 1;
            $num->user_daily = 1;
            $num->created_at = date('Y-m-d H:i:s', time());
            $num->updated_at = date('Y-m-d H:i:s', time());
            $num->save();
        } else {
            if (strtotime($num->created_at) >= strtotime($today)) {
                $num->user_num ++;
                $num->user_daily ++;
                $num->updated_at = date('Y-m-d H:i:s', time());
                $num->save();
            } else {
                $newNum = new UserNum();
                $newNum->user_num = $num->user_num + 1;
                $newNum->user_daily = 1;
                $newNum->created_at = date('Y-m-d H:i:s', time());
                $newNum->updated_at = date('Y-m-d H:i:s', time());
                $newNum->save();
            }
        }
    }
}
